﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SlimDX.DirectInput;

namespace WheelChairSimulation2
{
    class WheelChair
    {
        public Texture2D wheelChairTexture;
        private Vector2 position;

        //private MouseState mouseState;
        private Microsoft.Xna.Framework.Input.KeyboardState keyBoard;
        List<Joystick> joysticks = new List<Joystick>();
        List<DeviceInstance> directInputList = new List<DeviceInstance>();
        DirectInput directInput = new DirectInput();
        JoystickState joystickState = new JoystickState();

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        public WheelChair(SimEnvironment environment, Vector2 position)
        {
            this.position = position;
            wheelChairTexture = environment.content.Load<Texture2D>("wheelChair");
            Color[] pixelColors = new Color[wheelChairTexture.Width * wheelChairTexture.Height];
            //Color[,] pixelColors = new Color[wheelChair.Width,wheelChair.Height];
            wheelChairTexture.GetData<Color>(pixelColors);

            
            for (int i = 0; i < pixelColors.Length; i++)
            {
                float d = pixelColors[i].ToVector3().X;
                if (pixelColors[i].R == 1)
                {
                    Vector2 coords = new Vector2((int)i % wheelChairTexture.Width, (int)i / wheelChairTexture.Width);
                }
            }
            directInputList.AddRange(directInput.GetDevices(SlimDX.DirectInput.DeviceClass.GameController, SlimDX.DirectInput.DeviceEnumerationFlags.AttachedOnly));

            foreach (SlimDX.DirectInput.DeviceInstance obj in directInputList)
            {
                //joysticks.Capacity++;
                joysticks.Add(new SlimDX.DirectInput.Joystick(directInput, obj.InstanceGuid));
                joysticks[joysticks.Count - 1].Acquire();
            }
        }
        public WheelChair(Texture2D texture)
        {

        }
        public void Initialize(ContentManager content)
        {

        }
        public void Update(GameTime gameTime)
        {
            //mouseState = Mouse.GetState();
            keyBoard = Microsoft.Xna.Framework.Input.Keyboard.GetState();
            
            //if (keyBoard.IsKeyDown(Keys.F1))


            if (joysticks.Count > 0)
            {
                //joystickState = joysticks
                joystickState = joysticks[0].GetCurrentState();
                //joystickState = joysticks[0].GetForceSliders();

                if (joystickState.X > 34000)
                    position.X += 2;// rotate right.
                else if (joystickState.X < 31000)
                    position.X -= 2;// rotate left.

                if (joystickState.Y > 34000)
                    position.Y += 2;
                else if (joystickState.Y < 31000)
                    position.Y -= 2;
            }

        }

        protected void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            directInput.Dispose();
            foreach (Joystick joystick in joysticks)
            {
                joystick.Dispose();
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            //spriteBatch.Begin();
            spriteBatch.Draw(wheelChairTexture, position, Color.White);
            //spriteBatch.End();
        }
    }
}

